package lrl.nsfw.user.action;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import lrl.core.action.BaseAction;
import lrl.nsfw.role.service.RoleService;
import lrl.nsfw.user.entity.User;
import lrl.nsfw.user.entity.UserRole;
import lrl.nsfw.user.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends BaseAction {

	@Resource
	private UserService userService;
	@Resource
	private RoleService roleService;

	private List<User> userList;
	private User user;
	private File headImg;
	private String headImgContentType;
	private String headImgFileName;

	private File userExcel;
	private String userExcelContentType;
	private String userExcelFileName;

	private String[] userRoleIds;

	// 列表页面
	public String listUI() {
		userList = userService.findObjects();
		return "listUI";
	}

	// 跳转到新增页面
	public String addUI() {
		ActionContext.getContext().getContextMap()
				.put("roleList", roleService.findObjects());
		return "addUI";
	}

	// 保存新增
	public String add() {
		if (user != null) {
			if (headImg != null) {
				String filePath = ServletActionContext.getServletContext()
						.getRealPath("upload/user");
				String fileName = UUID.randomUUID().toString()
						.replaceAll("-", "")
						+ headImgFileName.substring(headImgFileName
								.lastIndexOf("."));
				try {
					FileUtils.copyFile(headImg, new File(filePath, fileName));
					user.setHeadImg("user/" + fileName);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			userService.saveUserAndRole(user, userRoleIds);
		}
		return "list";
	}

	// 跳转到编辑页面
	public String editUI() {
		ActionContext.getContext().getContextMap()
				.put("roleList", roleService.findObjects());
		if (user != null && user.getId() != null) {
			user = userService.findObjectById(user.getId());
			
			List<UserRole> list = userService.getUserRolesByUserId(user.getId());
			if (list != null && list.size() > 0) {
				userRoleIds = new String[list.size()];
				for (int i = 0; i < list.size(); i++) {
					userRoleIds[i] = list.get(i).getId().getRole().getRoleId();
				}
			}
		}
		return "editUI";
	}

	// 保存编辑
	public String edit() {
		if (user != null) {
			userService.update(user);
			if (headImg != null) {
				String filePath = ServletActionContext.getServletContext()
						.getRealPath("upload/user");
				String fileName = UUID.randomUUID().toString()
						.replaceAll("-", "")
						+ headImgFileName.substring(headImgFileName
								.lastIndexOf("."));
				try {
					FileUtils.copyFile(headImg, new File(filePath, fileName));
					user.setHeadImg("user/" + fileName);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			userService.updateUserAndRole(user, userRoleIds);
		}
		return "list";
	}

	// 删除
	public String delete() {
		if (user != null && user.getId() != null) {
			userService.delete(user.getId());
		}
		return "list";
	}

	// 批量删除
	public String deleteSelected() {
		if (selectedRow != null) {
			for (String id : selectedRow) {
				userService.delete(id);
			}
		}
		return "list";
	}

	// 导出用户列表
	public void exportExcel() {
		try {
			// 1、查找用户列表
			userList = userService.findObjects();
			// 2、导出
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/x-execl");
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String("用户列表.xls".getBytes(), "ISO-8859-1"));
			ServletOutputStream outputStream = response.getOutputStream();
			userService.exportExcel(userList, outputStream);
			if (outputStream != null) {
				outputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 校验用户名是否重复
	public void verifyAccount() {
		if (user != null && StringUtils.isNotBlank(user.getAccount())) {
			try {
				List<User> list = userService.findUserByAccountAndId(
						user.getAccount(), user.getId());
				String strResult = "true";
				if (list != null && list.size() > 0) {
					strResult = "false";
				}
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setContentType("html/text");
				ServletOutputStream outputStream = response.getOutputStream();
				outputStream.write(strResult.getBytes());
				outputStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public File getHeadImg() {
		return headImg;
	}

	public void setHeadImg(File headImg) {
		this.headImg = headImg;
	}

	public String getHeadImgContentType() {
		return headImgContentType;
	}

	public void setHeadImgContentType(String headImgContentType) {
		this.headImgContentType = headImgContentType;
	}

	public String getHeadImgFileName() {
		return headImgFileName;
	}

	public void setHeadImgFileName(String headImgFileName) {
		this.headImgFileName = headImgFileName;
	}

	public File getUserExcel() {
		return userExcel;
	}

	public void setUserExcel(File userExcel) {
		this.userExcel = userExcel;
	}

	public String getUserExcelContentType() {
		return userExcelContentType;
	}

	public void setUserExcelContentType(String userExcelContentType) {
		this.userExcelContentType = userExcelContentType;
	}

	public String getUserExcelFileName() {
		return userExcelFileName;
	}

	public void setUserExcelFileName(String userExcelFileName) {
		this.userExcelFileName = userExcelFileName;
	}

	public String[] getUserRoleIds() {
		return userRoleIds;
	}

	public void setUserRoleIds(String[] userRoleIds) {
		this.userRoleIds = userRoleIds;
	}

}
